package com.clz.latte.ui.loader;

import android.content.Context;
import android.support.v7.app.AppCompatDialog;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.clz.latte.R;
import com.clz.latte.utils.DimenUtil;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/11/23.
 */

public class LatteLoader {
    private static final ArrayList<AppCompatDialog> LOADERS = new ArrayList<>();
    private static final int LOADER_SCALE_SIZE = 8;
    private static final int LOADER_OFFSET_SCALE = 10;
    private static final String DEFALULT_TYPE = LoaderStyle.BallClipRotatePulseIndicator.name();

    public static void showLoading(Context context,Enum<LoaderStyle> styleEnum){
        showLoading(context,styleEnum.name());
    }

    public static void showLoading(Context context, String type) {
        AppCompatDialog dialog = new AppCompatDialog(context, R.style.dialog);
        AVLoadingIndicatorView avLoadingIndicatorView
                = LoaderCreator.create(type, context);
        dialog.setContentView(avLoadingIndicatorView);
        final Window dialogWindow = dialog.getWindow();
        final int screenWidth = DimenUtil.getScreenWidth();
        final int screenHeight = DimenUtil.getScreenHeight();
        if (dialogWindow != null) {
            final WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = screenWidth / LOADER_SCALE_SIZE;
            lp.height = screenHeight / LOADER_SCALE_SIZE;
            lp.height = lp.height + screenHeight / LOADER_OFFSET_SCALE;
            lp.gravity = Gravity.CENTER;
        }
        LOADERS.add(dialog);
        dialog.show();
    }

    public static void showLoading(Context context) {
        showLoading(context, DEFALULT_TYPE);
    }

    public static void stopLoading() {
        for (AppCompatDialog dialog : LOADERS) {
            if (dialog != null) {
                if (dialog.isShowing()) {
                    dialog.cancel();
                }
            }
        }
    }
}
